package com.jl.controller;

import com.jl.commons.base.BaseController;
import com.jl.commons.constant.Constants;
import com.jl.commons.constant.ErrorCode;
import com.jl.commons.constant.ErrorMessage;
import com.jl.commons.result.ResultData;
import com.jl.commons.shiro.ShiroUser;
import com.jl.model.User;
import com.jl.model.in.LoginUser;
import com.jl.service.IUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashSet;

@Controller
public class LoginController extends BaseController {

    @Autowired
    private IUserService userService;

    @GetMapping(value = "index")
    public String index() {
        return "index";
    }

    @GetMapping(value = "/login")
    public String login() {
        return "login";
    }

    @PostMapping(value="login")
    @ResponseBody
    public ResultData loginPost(String loginName, String password,HttpServletRequest request) {
        request.getSession(true);
        loginName = loginName.trim();
        Subject user = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(loginName, password);
        try {
            user.login(token);
            ShiroUser shiroUser = getShiroUser();
            return success(shiroUser);
        } catch (UnknownAccountException e) {
            return fail(ErrorCode.UN_KNOW_ACCOUNT,ErrorMessage.UN_KNOW_ACCOUNT);
        } catch (DisabledAccountException e) {
            return fail(ErrorCode.DISABLED_ACCOUNT,e.getMessage());
        } catch (IncorrectCredentialsException e) {
            return fail(ErrorCode.PASSWORD_ERROR,ErrorMessage.PASSWORD_ERROR);
        } catch (ExcessiveAttemptsException e) {
            return fail(ErrorCode.DISABLED_ACCOUNT,ErrorMessage.DISABLED_ACCOUNT);
        } catch (Throwable e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }


    @GetMapping(value = "/logout")
    public String logout(HttpServletResponse response, HttpServletRequest request) {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        HttpSession session = request.getSession();
        ServletContext context = session.getServletContext();
        HashSet<HttpSession> sessionSet = (HashSet<HttpSession>) context.getAttribute("sessionSet");
        if (sessionSet != null) {
            sessionSet.remove(session);
        }
        return "redirect:/index";
    }

}
